#!/bin/bash

# Application dependencies
DEPS="geomview mencoder convert"

# Application defaults
ANGULAR_INCREMENT=0
DELETE_DYN_FILES=0
ROTATE_AFTER=1
ROTATE_BEFORE=1
NO_SYM_ROT=10		# Rotation speed while not in sym
NO_SYM_N_LAPS=1	# How many laps should we do before/after sym
SCREEN_SIZE="640 480"

function halp() {
	echo -e "$0 usage:"
	echo -e "\t [-r=N] \t Rotation speed. Defaults to 0."
	echo -e "\t [-d]   \t Delete Dynamo files when done. Defaults to false."
	echo -e "\t [-b]   \t Rotate the camera 360 degrees before running the sym. Defaults to true."
	echo -e "\t [-a]   \t Rotate the camera 360 degrees after running the sym. Defaults to true."
	echo -e "\t [-s="X Y"] \t Video resolution. Defaults to 640 480."
	echo -e "$0 will scan the current directory for geomview*.list files and make a movie from them."
	exit
}

while getopts "hr:abds:" Option
do
  case $Option in
    h ) halp ;;
    r ) ANGULAR_INCREMENT=$OPTARG;;
    d ) DELETE_DYN_FILES=1;;
    a ) ROTATE_AFTER=0;;
    b ) ROTATE_BEFORE=0;;
    s ) SCREEN_SIZE=$OPTARG;;
    * ) echo "Unimplemented option chosen.";;   # Default.
  esac
done


# Check if every dependency for this script is met
for dep in $DEPS; do
	X=$(which $dep);
	if [ "$X" = "" ]; then
		echo "Dependency $dep is missing, can't run $0. Check 'apt-cache search $dep'.";
		exit
	fi
done


##################################
## Here starts the real mkmovie ##
##################################

# Do we have anything to process in this directory?
NUM_TO_PROCESS=$(ls geomview* 2>/dev/null | wc -l)
if [ $NUM_TO_PROCESS -lt 1 ]; then
	echo "Nothing to do here!"
	halp
fi

function rotate() {
	GCL_FILE=$1
	DYN_LIST_FILE=$2
	OUT_PREFIX=$3
	echo "Generating 360 view of $DYN_LIST_FILE"

	# Write GCL preamble defining the world
	echo -e "(window default { size $SCREEN_SIZE })" > $GCL_FILE

	STEPS=40
	ROT=$(echo "2 * 3.14159 / $STEPS" | bc -l)
	for i in $(seq 0 1 $STEPS ); do
		IPAD=$(printf "%03d" $i)
		echo "Generating $OUT_PREFIX$DYN_LIST_FILE$IPAD.ppm"
		echo -e "\n(load $DYN_LIST_FILE)\n(transform world world world rotate 0 $ROT 0)\n(zoom Camera 1)" >>  $GCL_FILE
		echo -e "\n(snapshot Camera $OUT_PREFIX$DYN_LIST_FILE$IPAD.ppm ppm)\n(delete g1)\n" >>  $GCL_FILE
	done

	echo "(quit)" >> $GCL_FILE
}

function create_gcl() {
	GCL_FILE=$1
	FILES_LIST=$2

	# Write GCL preamble defining the world
	echo -e "(window default { size $SCREEN_SIZE })" > $GCL_FILE

	# Process each frame
	J=0
	for file in $FILES_LIST; do
		echo "Processing frame $file"
		ROT=$(printf "%d.%02d" $(($J/100)) $(($J%100)) )
		J=$((J+ANGULAR_INCREMENT))
		echo -e "\n(load $file)\n(transform world world world rotate 0 $ROT 0)\n(zoom Camera 1)\n(snapshot Camera $file.ppm ppm)\n(delete g1)\n" >> $GCL_FILE
	done
	echo "(quit)" >> $GCL_FILE
}

# Create a full rotation of the camera before starting sym
if [ "$ROTATE_BEFORE" = "1" ]; then
	rotate takesnap.gcl $(ls geomview*|grep -v ppm|head -n1) "0"
	geomview -b 7 7 7 -nopanel -c takesnap.gcl 2>/dev/null
fi

create_gcl takesnap.gcl "$(ls geomview*)"
geomview -b 7 7 7 -nopanel -c takesnap.gcl 2>/dev/null

Create a full rotation of the camera after finishing the sym
if [ "$ROTATE_AFTER" = "1" ]; then
	rotate takesnap.gcl $(ls geomview*|grep -v ppm|tail -n1) "z"
	geomview -b 7 7 7 -nopanel -c takesnap.gcl 2>/dev/null
fi

# Encode all the files into a video
# TODO: There has to be a more direct way than list -> ppm -> png -> avi
echo "Converting images to PNG files..."
for f in *ppm ; do convert -quality 100 $f `basename $f ppm`png; done 
echo "Encoding new video..."
mencoder "mf://*.png" -mf fps=10 -o sym.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800 

# Delete temp files
rm -f *.ppm *.png takesnap.gcl
if [ "$DELETE_DYN_FILES" = "1" ]; then
	rm -f *.list 
fi

